package sg4c.frontend.command;

import java.io.IOException;
import sg4c.frontend.GraphicsContext;
import sg4c.protocol.DataInputProtocol;

public class DrawRect implements GraphicsCommand {

    private final int x;
    private final int y;
    private final int width;
    private final int height;
    
    public DrawRect(int x, int y, int w, int h) {
        this.x = x;
        this.y = y;
        this.width = w;
        this.height = h;
    }

    public void execute(GraphicsContext context) {
        if (!context.noFill()) {
            context.loadFillColor();
            context.getGraphics().fillRect(x, y, width, height);
        }
        if (!context.noStroke()) {
            context.loadStrokeColor();
            context.getGraphics().drawRect(x, y, width, height);
        }
        
    }
    public static final GraphicsCommand.Factory factory = new GraphicsCommand.Factory() {

        public GraphicsCommand parse(DataInputProtocol in) throws IOException {
            int x = in.readInt();
            int y = in.readInt();
            int w = in.readInt();
            int h = in.readInt();
            return new DrawRect(x, y, w, h);
        }
    };
}
